package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.PurchaseRequestItemMapper;
import com.kang.dao.PurchaseRequestMapper;
import com.kang.pojo.PurchaseItem;
import com.kang.pojo.PurchaseRequest;
import com.kang.service.PurchaseRequestService;
import com.vonechina.entity.page.PurchaseRequestItemPage;
import com.vonechina.entity.page.PurchaseRequestPage;

/**
 * 采购申请服务层实现类
 * 
 * @author zs614
 *
 */
@Service("PurchaseRequisitionService")
public class PurchaseRequestServiceImpl implements PurchaseRequestService {
	/**
	 * 退货申请持久层
	 */
	@Resource
	private PurchaseRequestMapper prm;
	/**
	 * 退货申请物品信息持久层
	 */
	@Resource
	private PurchaseRequestItemMapper itemMapper;

	/**
	 * 删除采购申请
	 * 
	 * @param purchaserequisitionid
	 *            采购申请ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer purchaserequisitionid) {
		return prm.deleteByPrimaryKey(purchaserequisitionid);
	}

	/**
	 * 通过主键查询采购申请
	 * 
	 * @param purchaseordereid
	 *            采购申请ID
	 * @return
	 */
	@Override
	public PurchaseRequest selectPurchaseRequestById(int purchaserequisitionid) {

		return prm.selectByPrimaryKey(purchaserequisitionid);
	}

	/**
	 * 分页查询采购申请
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<PurchaseRequest> listPurchaseRequest(PurchaseRequestPage page) {
		page.setRows(prm.countPurchaseRequest(page));
		return prm.listPurchaseRequest(page);
	}

	/**
	 * 提交采购申请
	 * 
	 * @param purchaserequisitionid
	 *            采购申请ID
	 * @return
	 */
	@Override
	public Integer submitRequest(Integer purchaserequisitionid) {
		return prm.submitRequest(purchaserequisitionid);
	}

	/**
	 * 保存修改状态
	 * 
	 * @param purchaserequisitionid
	 *            采购申请ID
	 * @param status
	 *            状态
	 * @return
	 */
	@Override
	public Integer updateStatus(Integer purchaserequisitionid, Integer status) {

		return prm.updateStatus(purchaserequisitionid, status);
	}

	/**
	 * 新增采购申请
	 * 
	 * @param pur
	 *            采购申请实体类
	 * @return
	 */
	@Override
	public Integer insertSelective(PurchaseRequest pur) {

		return prm.insertSelective(pur);
	}

	/**
	 * 生成采购申请编号
	 * 
	 * @return
	 */
	@Override
	public String generatoApplicationNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = prm.selectMaxApplication();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "SQ" + no;
	}

	/**
	 * 更新采购申请
	 * 
	 * @param record
	 *            采购申请实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKey(PurchaseRequest record) {
		return prm.updateByPrimaryKey(record);
	}

	/**
	 * 新增采购申请物品信息
	 * 
	 * @param item
	 *            采购申请物品信息
	 * @return
	 */
	@Override
	public int addItem(PurchaseItem item) {
		return itemMapper.addItem(item);
	}

	/**
	 * 更新采购申请物品信息
	 * 
	 * @param item
	 *            采购申请物品信息
	 * @return
	 */
	@Override
	public int updateItem(PurchaseItem item) {
		return itemMapper.updateItem(item);
	}

	/**
	 * 查询采购申请物品信息
	 * 
	 * @param item
	 *            采购申请物品信息
	 * @return
	 */
	@Override
	public PurchaseItem selectItem(PurchaseItem item) {
		return itemMapper.selectItem(item);
	}

	/**
	 * 查询采购申请物品
	 * 
	 * @param purchaseRequisitionID
	 *            采购申请ID
	 * @param warehouseManagementID仓库ID
	 * @return
	 */
	@Override
	public List<PurchaseItem> listItem(int purchaseRequisitionID, int warehouseManagementID) {
		return itemMapper.listItem(purchaseRequisitionID, warehouseManagementID);
	}

	/**
	 * 查询可用待采购物资
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<PurchaseItem> listNotInItem(PurchaseRequestItemPage page) {
		page.setRows(itemMapper.countNotInItem(page));
		return itemMapper.listNotInItem(page);
	}

}
